package com.lenin.metro.server;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.lenin.metro.client.GreetingService;
import com.lenin.metro.shared.MapCoordinates;
import com.lenin.metro.shared.MapObject;
import com.lenin.metro.shared.ProjectionPoint;
import com.lenin.metro.shared.Tile;
import com.lenin.metro.shared.TileGroup;
import com.lenin.metro.shared.TileMetrics;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	
	
	private DataStoreTest dsTest = new DataStoreTest();
	
	public void test(int id) {
		
		TileMetrics tm = TileMetrics.MetricsConfig.S32W5H4.getTileMetrics();
		
		if(id == 0) {
			dsTest.createTiles(new ProjectionPoint(0, 0), tm.getTileSize(), tm.getAreaWidth(), tm.getAreaHeight());
		} else if(id == 1) {
			dsTest.createTiles(new ProjectionPoint(0, 480), tm.getTileSize(), tm.getAreaWidth(), tm.getAreaHeight());
		} else if(id == 2) {
			dsTest.createTiles(new ProjectionPoint(480, 480), tm.getTileSize(), tm.getAreaWidth(), tm.getAreaHeight());
		} else if(id == 2) {
			dsTest.clearDb();
		}
		
	}
	
	
	
	public List<MapObject> getMapObjectsByCoords(MapCoordinates topLeftCoords, 
			MapCoordinates bottomRightCoords) {
		
		ProjectionPoint topLeft = MapCoordinates.coordsToPoint(topLeftCoords);
		ProjectionPoint bottomRight = MapCoordinates.coordsToPoint(bottomRightCoords);
		
		long tlX = topLeft.getX();
		long tlY = topLeft.getY();
		long brX = bottomRight.getX();
		long brY = bottomRight.getY();
		
		List<MapObject> dbObjects = new ArrayList<MapObject>();
		
		//MapObject dbObject = createMapObject();
		//dbObjects.add(dbObject);
		
		List<MapObject> observableObjects = new ArrayList<MapObject>();
		for(MapObject object : dbObjects) {
			
			ProjectionPoint objLoc = MapCoordinates.coordsToPoint(object.getCoordinates());
			ProjectionPoint objRectBr = object.getRectBottomRight();
			
			long objX = objLoc.getX();
			long objY = objLoc.getY();
			long objWidth = objRectBr.getX();
			long objHeight = objRectBr.getY();
			
			//System.out.println("tl=("+tlX+","+tlY+") :: br=("+brX+","+brY+") :: obj=("+objX+","+objY+")");
			if(objX+objWidth > tlX && objY+objHeight > tlY && objX < brX && objY < brY) {
				observableObjects.add(object);
			}
			
		}
		
		return observableObjects;
		
	}


	public List<TileGroup> getTileGroupsByCoords(List<MapCoordinates> coordsList, TileMetrics.MetricsConfig metrics) {
		
		List<TileGroup> tileGroups = new ArrayList<TileGroup>();
		
		for(MapCoordinates coords : coordsList) {
			
			TileGroup tileGroup = getTileGroupByCoords(coords, metrics);
			tileGroups.add(tileGroup);
		
		}
		
		return tileGroups;
		
	}
	
	
	public TileGroup getTileGroupByCoords(MapCoordinates coords, TileMetrics.MetricsConfig metrics) {
		
		TileMetrics tm = metrics.getTileMetrics();
		TileGroup tileGroup = dsTest.getTileGroup(coords, 
				tm.getTileSize(), tm.getGroupWidth(), tm.getGroupHeight());
		
		return tileGroup;
		
	}

	/*
	private MapObject createMapObject() {
		
		MapObject mapObject = new MapObject();
		
		MapCoordinates objectCoords = new MapCoordinates(0, 0);
		
		Tile[][] tiles = new Tile[1][3];
		Tile tile0 = new Tile(new MapCoordinates(0, 0));
		tiles[0][0] = tile0;
		Tile tile1 = new Tile(new MapCoordinates(0, 32));
		tiles[0][1] = tile1;
		Tile tile2 = new Tile(new MapCoordinates(0, 64));
		tiles[0][2] = tile2;
		
		mapObject.setCoordinates(objectCoords);
		mapObject.setTiles(tiles);
		mapObject.setTileSize(32);
		mapObject.setLatLength(32);
		mapObject.setLngLength(96);
		mapObject.setImageCss("bg-obj1");
		mapObject.setKey("821893812398");
		
		return mapObject;
	
	}
	*/

}
